package me.zingon.doc.service.impl;

import me.zingon.doc.model.User;
import me.zingon.doc.service.CatalogService;
import me.zingon.doc.dao.CatalogDao;
import me.zingon.doc.model.Catalog;
import me.zingon.doc.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* Created by ZTCGenerator<zingon@aliyun.com> on 2018-5-31.
*/
@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    CatalogDao catalogDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insert(Catalog catalog){
        return catalogDao.insert(catalog);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int delete(Catalog catalog){
        return catalogDao.delete(catalog);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteByIds(Integer[] ids) {
        return  catalogDao.deleteByIds(ids);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int update(Catalog catalog){
        return catalogDao.update(catalog);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Catalog getById(Integer id){
        return catalogDao.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<Catalog> list(Catalog catalog){
        return catalogDao.list(catalog);
    }

    @Override
    public List<Catalog> listByProjectIdWithUser(Integer projectId, User user) {
        return catalogDao.listByProjectIdWithUserId(projectId,user.getId());
    }

    @Override
    public int deleteByIdWithUser(Integer catalogId,Integer projectId,User user) {
        List<Catalog> catalogs = catalogDao.listByProjectIdWithUserId(projectId,user.getId());
        List<Integer> ids=new ArrayList<>();
        getChildren(new CatalogVO().setId(catalogId),catalogs,ids);
        ids.add(catalogId);
        Integer[] idstemp=new Integer[ids.size()];
        ids.toArray(idstemp);
        return catalogDao.logicDeleteByIds(idstemp);
    }


    @Override
    public List<CatalogVO> getChildren(CatalogVO cv, List<Catalog> catalogs,List<Integer> ids){
        List<CatalogVO> children =new ArrayList<>();
        for(Catalog c:catalogs){
            if(c.getParentId().equals(cv.getId())){
                ids.add(c.getId());
                CatalogVO child = new CatalogVO(c);
                children.add(child);
                getChildren(child,catalogs,ids);
            }
        }
        cv.setChildren(children);
        return children;
    }
}